window.addEventListener('load',function() {

	var submit = document.getElementsByName('submit')[0];
	
	var username = document.getElementById('register_username');
	var password = document.getElementById('register_password');
	var name 	 = document.getElementById('register_name');
	var email 	 = document.getElementById('register_email');
	var kelas 	 = document.getElementById('register_class');
	
	var errormessage1 = "Username yang kamu inginkan sudah terpakai";
	var errormessage2 = "Kolom Username dan Password tidak boleh kosong";
	
	document.getElementById('login_form').onsubmit = function() {
		//$('#register_username').attr("disabled", true);
		//$('#register_password').attr("disabled", true);
		
		$('.register_input').attr("disabled", true);
		
		/*
		alert(username.value);
		alert(password.value);
		alert(name.value);
		alert(email.value);
		alert(kelas.value); // */
		
		/*
		localStorage.setItem("estudia_user", "New User");
		localStorage.setItem("estudia_level", 1);
		localStorage.setItem("estudia_exp", 0);

		localStorage.setItem("estudia_mat_lesson1", 0);
		localStorage.setItem("estudia_mat_lesson2", 0);
		localStorage.setItem("estudia_mat_quiz1", 0);
		localStorage.setItem("estudia_mat_quiz2", 0);
		localStorage.setItem("estudia_fis_lesson1", 0);
		localStorage.setItem("estudia_fis_lesson2", 0);
		localStorage.setItem("estudia_fis_lesson3", 0);
		localStorage.setItem("estudia_fis_quiz1", 0);
		localStorage.setItem("estudia_fis_quiz2", 0);
		localStorage.setItem("estudia_fis_quiz3", 0);
		localStorage.setItem("estudia_bio_lesson1", 0);
		localStorage.setItem("estudia_bio_lesson2", 0);
		localStorage.setItem("estudia_bio_lesson3", 0);
		localStorage.setItem("estudia_bio_quiz1", 0);
		localStorage.setItem("estudia_bio_quiz2", 0);
		localStorage.setItem("estudia_bio_quiz3", 0);
		
		for(var i = 0; i <= 11; i++) {
			if (i < 10) {
				localStorage.setItem("estudia_achievment_b0" + i, 0);
			} else {
				localStorage.setItem("estudia_achievment_b" + i, 0);
			}
		}
		
		window.location.href = 'home.html';
		// */
		
		var result;
		$.get( "http://estudiahapid.ap01.aws.af.cm/services/register.php?username=" + username.value + "&password=" + password.value + "&name=" + name.value + "&email=" + email.value + "&class=" + kelas.value, function(data) {
			result = JSON.parse(data);
			//alert(result["status"]);
			if (result["status"] == "empty") {
				$('#errortext').empty();
				$('#errortext').append(errormessage2);
				$('#errortext').show();
				$('.register_input').attr("disabled", false);
				//$('#register_username').attr("disabled", false);
				//$('#register_password').attr("disabled", false);
			} else if (result["status"] == "used") {
				$('#errortext').empty();
				$('#errortext').append(errormessage1);
				$('#errortext').show();
				$('.register_input').attr("disabled", false);
				//$('#register_username').attr("disabled", false);
				//$('#register_password').attr("disabled", false);
			} else if (result["status"] == "success") {
				localStorage.setItem("estudia_register", "true");
				localStorage.setItem("estudia_user", username.value);
				localStorage.setItem("estudia_level", 1);
				localStorage.setItem("estudia_exp", 0);
				
				for(var i = 0; i <= 11; i++) {
					if (i < 10) {
						localStorage.setItem("estudia_achievment_b0" + i, 0);
					} else {
						localStorage.setItem("estudia_achievment_b" + i, 0);
					}
				}
				
				localStorage.setItem("estudia_mat_lesson1", 0);
				localStorage.setItem("estudia_mat_lesson2", 0);
				localStorage.setItem("estudia_mat_quiz1", 0);
				localStorage.setItem("estudia_mat_quiz2", 0);
				localStorage.setItem("estudia_fis_lesson1", 0);
				localStorage.setItem("estudia_fis_lesson2", 0);
				localStorage.setItem("estudia_fis_lesson3", 0);
				localStorage.setItem("estudia_fis_quiz1", 0);
				localStorage.setItem("estudia_fis_quiz2", 0);
				localStorage.setItem("estudia_fis_quiz3", 0);
				localStorage.setItem("estudia_bio_lesson1", 0);
				localStorage.setItem("estudia_bio_lesson2", 0);
				localStorage.setItem("estudia_bio_lesson3", 0);
				localStorage.setItem("estudia_bio_quiz1", 0);
				localStorage.setItem("estudia_bio_quiz2", 0);
				localStorage.setItem("estudia_bio_quiz3", 0);
				
			}
			
		}).done( function(data) {
			if (result["status"] == "success") {
				window.location.href = 'home.html';
			}
		});
		// */

		return false;
	};	
	
});
